package com.windy.action;

import java.util.HashMap;
import java.util.List;

import com.windy.util.TransportObject;

@SuppressWarnings("unchecked")
public abstract class PaginationAction extends BaseAction {
	private int curPage;//start from 1
	private int pageSize;
	private int totalRecords;
	private String actionPath;
	private String doMethod;
	private HashMap<Object, Object> paramMap;
	private List resultList;
	
	public final String execute()
	{
		if(doMethod.equalsIgnoreCase("first"))
		{
			curPage = 1;
		}
		else if(doMethod.equalsIgnoreCase("prev"))
		{
			if(curPage > 1)
			{
				curPage--;
			}
		}
		else if(doMethod.equalsIgnoreCase("next"))
		{
			int last = (totalRecords/pageSize)+((totalRecords%pageSize)>0?1:0);
			if(curPage < last)
			{
				curPage++;
			}
		}
		else if(doMethod.equalsIgnoreCase("last"))
		{
			curPage = (totalRecords/pageSize)+((totalRecords%pageSize)>0?1:0);
		}
		else
		{
			
		}
		int start = (curPage-1)*pageSize;
		
		TransportObject searchResult = doPaginationSearch(start, pageSize);
		this.totalRecords = searchResult.getTotalRecords();
		
		return SUCCESS;
	}
	
	public abstract TransportObject doPaginationSearch(int start, int size);
	public abstract void buildParamMap();


	/*************************************************/

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

	public String getActionPath() {
		return actionPath;
	}

	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}

	public HashMap<Object, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(HashMap<Object, Object> paramMap) {
		this.paramMap = paramMap;
	}

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public void setDoMethod(String doMethod) {
		this.doMethod = doMethod;
	}
	
	
}
